
#include "../include/server.h"
int main(int argc, const char *argv[])
{
	//打开服务器数据库
	sqlite3 * db;
	if(-1 == sqlite3_open("my.db",&db))
	{
		perror("sqlite3_open");
		exit(-1);
	}

	//创建监听套接字
	int listenfd = socket(AF_INET,SOCK_STREAM,0);
	if(-1 == listenfd)
	{
		perror("listenfd");
		exit(-1);
	}

	//绑定本机地址并进入监听态
	struct sockaddr_in myaddr = {0};
	myaddr.sin_family = AF_INET;
	myaddr.sin_port = htons(8888);
	myaddr.sin_addr.s_addr = inet_addr("0.0.0.0");
	int mylen = sizeof myaddr;

	if(-1 == (bind(listenfd,(struct sockaddr *)&myaddr,mylen)))
	{
		perror("bind");
		exit(-1);
	}

	if(-1 == (listen(listenfd,10)))
	{
		perror("listen");
		exit(-1);
	}

	//多线程并发接收处理客户端数据
	pid_t pid;
	struct sockaddr_in clientaddr = {0};
	int clientlen;

	parg_t myarg = {0};
	myarg.pg_mydb = db;
	while(1)
	{
		int sockfd = accept(listenfd,(struct sockaddr *)&clientaddr,&clientlen);
		myarg.pg_sockfd = sockfd;
		if(-1 == sockfd)
		{
			perror("accept");
			exit(-1);
		}

		pthread_create(&pid,NULL,func,(void *)&myarg);
	}
	return 0;
}
